home *** CD-ROM | disk | FTP | other *** search
- Unit Outlook;
-
- Interface
-
- Uses Outlook8;
-
- Const
- CRLF = #13#10;
-
- Type
- CalendarCache = Array[1..31] of String;
-
- TOutlookObjects = Class(TObject)
- Protected
- nsMAPI : NameSpace;
- { calendar }
- mfCalendar : MAPIFolder;
- itAppointments : Items;
- ccCurrentMonth : CalendarCache;
- iCacheYear : Integer;
- iCacheMonth : Integer;
- { contacts }
- mfContacts : MAPIFolder;
- itContacts : Items;
- { tasks }
- mfTasks : MAPIFolder;
- itTasks : Items;
- { Notes }
- mfNotes : MAPIFolder;
- itNotes : Items;
- { Inbox }
- mfInbox : MAPIFolder;
- itInbox : Items;
- Procedure CheckApplicationConnection;
- Procedure BuildCalendarCache(iYear,iMonth : Integer);
- Function FixStringIfNotEmpty(strString : String; strPrefix : String = '') : String;
- Public
- Constructor Create;
- Procedure Free;
- Function GetAppointmentsOn(iYear,iMonth,iDay : Integer) : String;
- Function GetContactCount : Integer;
- Function GetContactDetails(iContactNumber : Integer) : String;
- Function GetTaskCount : Integer;
- Function GetTaskDetails(iTaskNumber : Integer) : String;
- Function GetNoteCount : Integer;
- Function GetNoteDetails(iNoteNumber : Integer) : String;
- Function GetInboxMessageCount : Integer;
- Function GetInboxMessageDetails(iMessageNumber : Integer) : String;
- End;
-
- Implementation
-
- { TOutlookObjectsCalendar }
-
- Uses SysUtils,Windows,OLEServer,MainForm;
-
- Var
- oaApplication : TOutlookApplication;
-
- Constructor TOutlookObjects.Create;
- Begin
- CheckApplicationConnection;
- Try { navigate to the appropriate objects }
- With oaApplication do Begin
- nsMAPI := GetNamespace('MAPI');
- nsMAPI.Logon('',EmptyParam,EmptyParam,EmptyParam); { logon as default user }
- mfCalendar := nsMAPI.GetDefaultFolder(olFolderCalendar);
- itAppointments := mfCalendar.Items;
- mfContacts := nsMAPI.GetDefaultFolder(olFolderContacts);
- itContacts := mfContacts.Items;
- mfTasks := nsMAPI.GetDefaultFolder(olFolderTasks);
- itTasks := mfTasks.Items;
- mfNotes := nsMAPI.GetDefaultFolder(olFolderNotes);
- itNotes := mfNotes.Items;
- mfInbox := nsMAPI.GetDefaultFolder(olFolderInbox);
- itInbox := mfInbox.Items;
- End;
- Except
- Fail; { fail the constructor }
- End;
- End;
-
- Procedure TOutlookObjects.Free;
- Begin
- Try
- { free all interfaces }
- itAppointments := nil;
- mfCalendar := nil;
- itContacts := nil;
- mfContacts := nil;
- nsMAPI.Logoff;
- nsMAPI := nil;
- Except
- MessageBeep(0);
- End;
- If (oaApplication <> nil) Then Begin
- oaApplication.Disconnect;
- oaApplication.Free;
- End;
- End;
-
- Procedure TOutlookObjects.CheckApplicationConnection;
- Begin
- If (oaApplication = nil) Then Begin
- oaApplication := TOutlookApplication.Create(nil);
- oaApplication.ConnectKind := ckRunningOrNew;
- oaApplication.Connect;
- End;
- End;
-
- Procedure TOutlookObjects.BuildCalendarCache(iYear,iMonth : Integer);
- Var
- iIndex,iDay : Integer;
- aiItem : AppointmentItem;
- dtFirstDay : TDateTime;
- dtLastDay : TDateTime;
- wY,wM,wD : Word;
- iStart,IEnd : Integer;
- strSubject : String;
-
- Function GetRealEndDate : TDateTime;
- Begin
- Result := aiItem.End_;
- { all-day events end at next day 00:00 so decrement the day number }
- If aiItem.AllDayEvent Then Result := Result-1;
- End;
-
- Function OccursInTheGivenMonth : Boolean;
- Begin
- Result := False;
- If ((aiItem.Start < dtFirstDay) And (GetRealEndDate < dtFirstDay)) Then Exit; { occurs in the past }
- If (aiItem.Start > dtLastDay) Then Exit; { occurs in the future }
- Result := True;
- End;
-
- Begin
- { do we need to build the cache? }
- If ((iYear <> iCacheYear) Or (iMonth <> iCacheMonth)) Then Begin
- Try
- { first clear the cache }
- For iIndex := 1 to 31 do ccCurrentMonth[iIndex] := '';
- { start and end dates for the given iMonth }
- dtFirstDay := EncodeDate(iYear,iMonth,1);
- dtLastDay := EncodeDate(iYear,iMonth,MonthDays[IsLeapYear(iYear),iMonth]);
- { then build the cache by enumerating all appointments }
- For iIndex := 1 to itAppointments.Count do Begin
- aiItem := AppointmentItem(itAppointments.Item(iIndex)); { typecast }
- { does the appointment occur in the given iMonth? }
- If OccursInTheGivenMonth Then Begin
- { decode the start and end dates }
- DecodeDate(aiItem.Start,wY,wM,wD);
- { did the event start in the past iMonth(s)? }
- If (wM < iMonth) Then iStart := 1
- Else iStart := wD;
- DecodeDate(GetRealEndDate,wY,wM,wD);
- { did the event end in the future iMonth(s)? }
- If (wM > iMonth) Then iEnd := MonthDays[IsLeapYear(iYear),iMonth]
- Else iEnd := wD;
- { save the appointment in the cache }
- strSubject := aiItem.Subject;
- For iDay := iStart to iEnd do
- ccCurrentMonth[iDay] := ccCurrentMonth[iDay]+strSubject+'<BR>';
- End;
- aiItem := nil;
- End;
- { save the currently caches time period }
- iCacheYear := iYear;
- iCacheMonth := iMonth;
- Finally
- aiItem := nil;
- End;
- End;
- End;
-
- Function TOutlookObjects.GetAppointmentsOn(iYear,iMonth,iDay : Integer) : String;
- Begin
- BuildCalendarCache(iYear,iMonth);
- Result := ccCurrentMonth[iDay];
- End;
-
- Function TOutlookObjects.FixStringIfNotEmpty(strString : String; strPrefix : String = '') : String;
- Begin
- If (strString <> '') Then strString := strPrefix+strString+'<BR>'+CRLF;
- Result := strString;
- End;
-
- Function TOutlookObjects.GetContactCount : Integer;
- Begin
- Result := itContacts.Count;
- End;
-
- Function TOutlookObjects.GetContactDetails(iContactNumber : Integer) : String;
- Var ciItem : ContactItem;
- Begin
- ciItem := ContactItem(itContacts.Item(iContactNumber+1));
- Result := '<B>'+ciItem.FileAs+'</B><BR>'+CRLF+
- FixStringIfNotEmpty(ciItem.CompanyName)+
- FixStringIfNotEmpty(ciItem.MailingAddress)+
- FixStringIfNotEmpty(ciItem.BusinessTelephoneNumber,'b. ')+
- FixStringIfNotEmpty(ciItem.HomeTelephoneNumber,'h. ')+
- FixStringIfNotEmpty(ciItem.MobileTelephoneNumber,'m. ')+
- '<A HREF="mailto:'+ciItem.Email1Address+'">'+ciItem.Email1Address+'</A><BR> ';
- End;
-
- Function TOutlookObjects.GetTaskCount : Integer;
- Begin
- Result := itTasks.Count;
- End;
-
- Function TOutlookObjects.GetTaskDetails(iTaskNumber : Integer) : String;
- Const
- cstrTaskStatuses : Array[olTaskNotStarted..olTaskDeferred] of String =
- ('Not started','In progress','Complete','Waiting','Deferred');
-
- Var
- tiItem : TaskItem;
-
- Function SafeDateToStr(dtDate : TDateTime) : String;
- Begin
- If (dtDate < 1) Then Result := '' { don't show dates from 19th century }
- Else Result := DateToStr(dtDate);
- End;
-
- Begin
- tiItem := TaskItem(itTasks.Item(iTaskNumber+1));
- Result := '<B>'+tiItem.Subject+'</B><BR>'+CRLF+
- FixStringIfNotEmpty(SafeDateToStr(tiItem.DueDate),'Due: ')+
- FixStringIfNotEmpty(SafeDateToStr(tiItem.StartDate),'Start: ')+
- cstrTaskStatuses[Integer(tiItem.Status)]+'<BR>'+CRLF+
- 'Complete: '+IntToStr(tiItem.PercentComplete)+'%<BR>'+CRLF;
- End;
-
- Function TOutlookObjects.GetNoteCount : Integer;
- Begin
- Result := itNotes.Count;
- End;
-
- Function TOutlookObjects.GetNoteDetails(iNoteNumber : Integer) : String;
- Var niItem : NoteItem;
- Begin
- niItem := NoteItem(itNotes.Item(iNoteNumber+1));
- Result := ' <TD WIDTH="30%"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B>'+
- niItem.Subject+'</B></FONT></TD>'+CRLF+
- ' <TD WIDTH="70%"><PRE><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">'+
- niItem.Body+'</FONT></PRE></TD>'+CRLF;
- End;
-
- Function TOutlookObjects.GetInboxMessageCount : Integer;
- Begin
- Result := itInbox.Count;
- End;
-
- Function TOutlookObjects.GetInboxMessageDetails(iMessageNumber : Integer) : String;
- Var miItem : MailItem;
- Begin
- miItem := MailItem(itInbox.Item(iMessageNumber+1));
- Result := ' <TR BGCOLOR="#EEEEEE">'+CRLF+
- ' <TD><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B>From: '+
- miItem.SenderName+'</B><BR>'+CRLF+
- ' Subject: '+miItem.Subject+'<BR>'+CRLF+
- ' Received: '+DateTimeToStr(miItem.ReceivedTime)+'</FONT></TD>'+CRLF+
- ' </TR>'+CRLF+
- ' <TR>'+CRLF+
- ' <TD><PRE><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">'+
- miItem.Body+'</FONT></PRE></TD>'+CRLF+
- ' </TR>'+CRLF;
- End;
-
- End.
-